<?php
/**
 * 	app后台
 *  =============================
 * 	后台父类
 * 	=============================
 *  Date: 2017-12-06
 */
namespace app\walletyhbbadmin\controller;
use think\Controller;
use think\Session;
use think\Db;

class AdminBase extends Controller {
    /*
     * 初始化操作
     */
    public function _initialize() {
    	parent::_initialize();
    	//管理员信息
    	$admin_id = session('admin_id');
    	//验证登录
    	if (empty($admin_id)){
    		$this->redirect('Login/index');
    	}
    	$where['admin_id'] = $admin_id;
    	$admin_user = Db::name("Admin")->where($where)->find();
    	$this->admin = $admin_user;
    	//配置项
//     	$config = Db::name("app_config")->field('key,value')->select();
//     	foreach ($config as $k=>$v){
//     		$config[$v['key']]=$v['value'];
//     	}
//     	$this->config=$config;
    	//**************权限管理***********
    	$adminquanxian= Db::name("Admin")->field('nav_list')->where(array('admin_id'=>$admin_id))->find();
    	$rules = $adminquanxian['nav_list'];
    	if(empty($rules)){
    		$this->error('此账号尚未分配权限');
    	}
    	$rules=explode(',', $rules);
    	foreach ($rules as $k=>$v){
    		$list[]=Db::name("app_nav")->where('nav_id='.$v)->find();
    	}
    	foreach ($list as $k=>$v){
    		$v['nav_url'] = '/walletyhbbadmin'.$v['nav_url'];
    		$value[$v['cat_id']][]=$v;
    	}
    	foreach ($value as $k=>$v){
    		$this->assign($k."_nav",$v);
    	}

    }
    /**
     * 获得一个6位的数字字母（唯一）
     */
    public function getTuiJianCode(){
    	$str = rand('10000000','99999999');
    	$member = M('Users')->where(array('code'=>$str))->find();
    	if($member){
    		$str = $this->getTuiJianCode();
    	}
    	return $str;
    }
    
    /**
     * 获得配置项
     * @return unknown
     */
    public function getConfig(){
    	$list=M('App_config')->select();
    	foreach ($list as $k=>$v){
    		$list[$v['key']]=$v['value'];
    	}
    	return $list;
    }
    
    
    public function regex($rule,$value) {
        $validate = array(
                'email'     =>  '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
                'url'       =>  '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
                'currency'  =>  '/^\d+(\.\d+)?$/',
                'number'    =>  '/^\d+$/',
                'num'       =>  '/^[0-9]*$/',
                'zip'       =>  '/^\d{6}$/',
                'integer'   =>  '/^[-\+]?\d+$/',
                'double'    =>  '/^[-\+]?\d+(\.\d+)?$/',
                'english'   =>  '/^[A-Za-z]+$/',
                'idcard'    =>  '/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i' ,
                //          'name'      =>  '/[^\D]/g',
                'img'       =>  '/\.(jpg|gif|png)$/i',
                'phone'     =>  '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#',
                'password'  =>  '/^[\w-\.]{6,16}$/',
                'bankcard'  =>  '/^(\d{16}|\d{19})$/',
                'username'  =>  '/^[a-zA-Z][a-zA-Z0-9_]{1,7}$/',
                'money'     =>  '/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/',
        );
        // 检查是否有内置的正则表达式
        if(isset($validate[strtolower($rule)])){
            $rule       =   $validate[strtolower($rule)];
        }
        return preg_match($rule,$value);
    }

    /**
     * 管理员操作日志
     */
    public function daily($content)
    {
        $data['admin_id'] = session('admin_id');
        $data['log_info'] = $content;
        $data['login_time'] = time();
        $data['log_ip'] =  $_SERVER["REMOTE_ADDR"];
        $data['log_url'] = $_SERVER['REQUEST_URI'];
        $data['log_time'] = time();
        $rr = DB::name('Admin_log')->insert($data);
    }
}